﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Content;
using DotNetNuke.Entities.Content.Common;

namespace Ivevane.Modules.Newslider.Components.Taxonomy
{
    public class Content
    {
        private const string ContentTypeName = "NewsliderArticle";

        public ContentItem CreateContentItem(Article objArcticle, int tabId)
        {
            var typeController = new ContentTypeController();
            var colContentTypes = (from a in typeController.GetContentTypes() where a.ContentType == ContentTypeName select a);
            int contentTypeId;

            if (colContentTypes.Count() > 0)
            {
                var contentType = colContentTypes.Single();
                contentTypeId = contentType == null ? CreateContentType() : contentType.ContentTypeId;
            }
            else
            {
                contentTypeId = CreateContentType();
            }

            var objContent = new ContentItem
            {
                Content = objArcticle.Title + " " + objArcticle.Description,
                ContentTypeId = contentTypeId,
                Indexed = false,
                ContentKey = "aid=" + objArcticle.ItemId,
                ModuleID = objArcticle.ModuleId,
                TabID = tabId
            };

            objContent.ContentItemId = Util.GetContentController().AddContentItem(objContent);

            // Add Terms
            var cntTerm = new Terms();
            cntTerm.ManageArticleTerms(objArcticle, objContent);

            return objContent;
        }

        public void UpdateContentItem(Article objArticle, int tabId)
        {
            var objContent = Util.GetContentController().GetContentItem(objArticle.ContentItemId);

            if (objContent == null) return;
            objContent.Content = objArticle.Title + " " + objArticle.Description;
            objContent.TabID = tabId;
            Util.GetContentController().UpdateContentItem(objContent);

            // Update Terms
            var cntTerm = new Terms();
            cntTerm.ManageArticleTerms(objArticle, objContent);
        }

        public void DeleteContentItemId(Article objArticle)
        {
            if (objArticle.ContentItemId <= Null.NullInteger) return;
            var objContent = Util.GetContentController().GetContentItem(objArticle.ContentItemId);
            if (objContent == null) return;

            // remove any metadata/terms
            var cntTerms = new Terms();
            cntTerms.RemoveArticleTerms(objArticle);

            Util.GetContentController().DeleteContentItem(objContent);
        }

        #region Private Methods

        ///<summary>
        ///Creates a Content Type (for taxonomy) in the data store.
        ///</summary>
        ///<returns> The primary Key of the new ContentType.</returns>
        private static int CreateContentType()
        {
            var typeController = new ContentTypeController();
            var objContentType = new ContentType { ContentType = ContentTypeName };

            return typeController.AddContentType(objContentType);
        }
        #endregion
    }
}